﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Linq;

namespace ABELGX
{
    public class EnumItem
    {
        public int ID { get; set; }
        public string Name { get; set; }
        public string DisplayName { get; set; }
        public string Description { get; set; }
        public string Category { get; set; }
        public EnumItem()
        {

        }

        /// <summary>
        /// 读取枚举数组返回枚举对象信息
        /// </summary>
        /// <param name="enumArray"></param>
        /// <returns></returns>
        public static List<EnumItem> GetEnumDesc(Array enumArray)
        {
            List<EnumItem> eis = new List<EnumItem>();
            foreach (Enum e in enumArray)
            {
                eis.Add(GetEnum(e));
            }
            return eis;
        }
        /// <summary>
        /// 获取枚举对象
        /// </summary>
        /// <returns></returns>
        public static EnumItem GetEnum(Enum e)
        {
            EnumItem ei = new EnumItem();
            ei.ID = Convert.ToInt32(e);
            ei.Name = e.ToString();
            FieldInfo EnumInfo = e.GetType().GetField(ei.Name);
            DisplayAttribute display = EnumInfo.GetCustomAttribute<DisplayAttribute>();
            ei.DisplayName = display?.Name;
            DescriptionAttribute descripton = EnumInfo.GetCustomAttribute<DescriptionAttribute>();
            ei.Description = descripton?.Description;
            CategoryAttribute category = EnumInfo.GetCustomAttribute<CategoryAttribute>();
            ei.Category = category?.Category;
            return ei;
        }
    }
}
